package com.czl.controller;

import com.czl.entity.Student;
import com.czl.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.PageResult;
import utils.R;

import java.util.List;


@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    StudentService studentsService;

    @GetMapping("/list.do")
    public List<Student> getAll(){
        return studentsService.selectAll();
    }

    @GetMapping("/selectByPage.do")
    public PageResult<Student> selectByPage(@RequestParam(name = "currentPage",defaultValue = "1") Integer page,
                                            @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){

       return studentsService.selectByPage(page,pageSize);
    }
    @PostMapping("/selectByCondition.do")
    public PageResult<Student> selectCondition(@RequestBody(required = false) Student student,
                                               @RequestParam(name = "currentPage",defaultValue = "1") Integer page,
                                               @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        System.out.println(student);
        return studentsService.selectByCondition(page,pageSize,student);
    }
    @PostMapping("/addStudent.do")
    public R addStudent(@RequestBody Student student){
        return studentsService.addStudent(student) > 0 ? R.ok().message("添加成功") : R.error().message("添加失败");
    }


}
